package com.gewu.pm.ui.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.activity.filter.FilterMultipleActivity;
import com.gewu.pm.ui.fragment.report.DailyReportFragment;
import com.hjq.bar.TitleBar;
import com.hjq.base.FragmentPagerAdapter;

/**
 * 日报管理页面
 */
public final class DailyReportActivity extends AppActivity {

    private TitleBar mTitleBar;
    private EditText mSearchEdit;
    private TextView mWriteReportBtn;
    private XTabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;

    private String mSearchKeyword = "";

    public static void start(Context context) {
        Intent intent = new Intent(context, DailyReportActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_daily_report;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.tb_title);
        mSearchEdit = findViewById(R.id.et_search);
        mWriteReportBtn = findViewById(R.id.tv_write_report);
        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.vp_content);

        // 设置标题栏
        mTitleBar.setOnTitleBarListener(this);

        // 设置搜索框提示
        mSearchEdit.setHint("搜索发布人");

        // 设置搜索监听
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mSearchKeyword = s.toString().trim();
                // TODO: 搜索功能
            }
        });

        // 设置写日报按钮点击
        setOnClickListener(mWriteReportBtn);

        // 创建 FragmentPagerAdapter
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        
        // 添加4个Fragment（都是同一个Fragment）
        mPagerAdapter.addFragment(DailyReportFragment.newInstance(), getString(R.string.all));
        mPagerAdapter.addFragment(DailyReportFragment.newInstance(), getString(R.string.my_own));
        mPagerAdapter.addFragment(DailyReportFragment.newInstance(), getString(R.string.mentioned_me));
        mPagerAdapter.addFragment(DailyReportFragment.newInstance(), getString(R.string.subordinate));
        
        // 设置 Adapter
        mViewPager.setAdapter(mPagerAdapter);
        
        // 关联 TabLayout 和 ViewPager
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void initData() {
        // 初始化数据
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_write_report) {
            // 跳转到写日报页面
            startActivity(new Intent(this, WriteDailyReportActivity.class));
        }
    }

    @SingleClick
    @Override
    public void onRightClick(View view) {
        // 点击筛选按钮
        // TODO: 跳转到筛选页面
        FilterMultipleActivity.start(getActivity());
    }
}

